#include "relay.h"
#include "log.h"

#define LOG_LOCAL_LEVEL LOG_DEBUG
#define TAG  "RELAY:"

void relay_init ( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOA, ENABLE);        //打开LED GPIO系统时钟

  //配置LED GPIO
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6; //LED0¡¢LED1ºÍLED2¶ÔÓ¦IO¿Ú
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                  //配置为输出功能
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                 //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             //50MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                   //上拉
  GPIO_Init (GPIOA, &GPIO_InitStructure);                        //GPIO进行初始化

  GPIO_ResetBits (GPIOA, GPIO_Pin_5|GPIO_Pin_6);    //默认为低,关闭继电器
}

void relay_ctrl(uint8_t index, uint8_t status)
{
    uint16_t  _io = 0;
    if (index == LEFT_RELAY)
        _io = GPIO_Pin_5;
    else if (index == RIGHT_RELAY)
        _io = GPIO_Pin_6;

    if (status == RELAY_ON) {
        GPIO_SetBits(GPIOA, _io);
        LOGD("set %s relay on",(index == LEFT_RELAY)? "left":"right");
    } else if (status == RELAY_OFF) {
        GPIO_ResetBits(GPIOA, _io);
        LOGD("set %s relay off",(index == LEFT_RELAY)? "left":"right");
    }
}
